<?php

namespace Admin\Controller;

use Think\Controller;

/*
 * @author guokengle from bayue.com
 * @desc 用户端管理
 * */

class UserController extends BaseController
{

    /**
     * 注册来源
     * @param $k
     * @return mixed
     */
    private function source($k)
    {
        $list[1] = 'app';
        $list[2] = '推荐';
        return $list[$k];
    }

    /**
     * 角色类型
     * @param $k
     * @return mixed
     */
    private function role($k)
    {
        $list[1] = '普通用户';
        $list[2] = '博主';
        return $list[$k];
    }

    /**
     * 性别
     * @param $k
     * @return mixed
     */
    private function sex($k)
    {
        $list[1] = '男';
        $list[2] = '女';
        $list[3] = '未知';
        return $list[$k];
    }

    //实名认证状态
    private function realname_status($k)
    {
        $lis[0] = '初始化';
        $lis[1] = '正常';
        $lis[2] = '被拒绝';
        $lis[3] = '待审核';
        return $lis[$k];
    }

    //博主审核状态
    private function bo_status($k)
    {
        $lis[0] = '初始化';
        $lis[1] = '正常';
        $lis[2] = '审核未通过';
        $lis[3] = '审核中';
        return $lis[$k];
    }

    //博主用户身份
    private function user_type($k)
    {
        $lis[1] = '个人';
        $lis[2] = '机构';
        return $lis[$k];
    }


    /**状态属性
     * @param $k
     * @return mixed
     */
    private function sta($k)
    {
        $list[0] = '禁用';
        $list[1] = '正常';
        return $list[$k];
    }

    /**
     * 用户列表
     */
    public function index()
    {

        $u = $_SERVER['REQUEST_URI'];
        $url = substr($u, (strpos($u, '=') + 1), (strlen($u) - strpos($u, '=') - 6));
        $lis = explode('/', $url);
        $mmp['controller'] = $lis[2];
        $mmp['function'] = $lis[3];
        $res = M('nav')->where($mmp)->getField("id");
        $role_id = $_SESSION['adminster']['role_id'];
        if (!$role_id == 0) {
            $path = M('access')->where("role_id = " . $role_id)->getField("nav_id");
            $pathlist = explode('#', $path);
            if (!in_array($res, $pathlist)) {
                $this->error('没有权限!', U('Index/main'), 1);
                return false;
            }
        }
        $nickname = I('nickname');
        $Disable=I('Disable');
        $Realname=I('Realname');
        if ($nickname) {
            $m = M('user');
            $mm['nickname'] = array('like', "%{$nickname}%");
            $mm['u_no|id|phone'] = array('eq', $nickname);
            $mm['_logic']='or';
            $where['_complex'] =$mm;
            $where['role'] = array('eq', 1);
            $count = $m->where($where)->count();
            $page = new \Think\Page($count, 10);
            $show = $page->show();
            $list = $m->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("id desc")->select();
        } elseif($Disable or $Realname){
           if ($Disable==1){
               $m = M('user');
               $where['role'] = array('eq', 1);
               $where['status'] = array('eq', 0);
               $count = $m->where($where)->count();
               $page = new \Think\Page($count, 10);
               $show = $page->show();
               $list = $m->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("id desc")->select();
           }
            if ($Realname==1){
                $m = M('user');
                $where['role'] = array('eq', 1);
                $where['realname_status'] = array('neq', 1);
                $count = $m->where($where)->count();
                $page = new \Think\Page($count, 10);
                $show = $page->show();
                $list = $m->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("id desc")->select();
            }

        }else{
            $m = M('user');
            $where['role'] = array('eq', 1);
            $count = $m->where($where)->count();
            $page = new \Think\Page($count, 10);
            $show = $page->show();
            $list = $m->where($where)->limit($page->firstRow . ',' . $page->listRows)->order("id desc")->select();
        }
        foreach ($list as &$val) {
            $val['source'] = $this->source($val['source']);
            $val['role'] = $this->role($val['role']);
            $val['sex'] = $this->sex($val['sex']);
            $val['sta'] = $this->sta($val['status']);
            $val['reg_time'] = date('Y/m/d H:i:s', $val['reg_time']);
            $val['last_time'] = date('Y/m/d H:i:s', $val['last_time']);
            $val['birthdaytime'] = date('Y/m/d H:i:s', $val['birthdaytime']);
        }

        $this->assign('show', $show);
        $this->assign('list', $list);
        $this->display();
    }

    //资金变动具体类型
    private function code($k)
    {
        $lis[1] = "充值";
        $lis[2] = "提现";
        $lis[10] = "视频付费";
        $lis[11] = "直播间送礼物";
        $lis[12] = "视频直播付费";
        $lis[20] = "音频付费";
        $lis[21] = "音频直播送礼物";
        $lis[22] = "音频直播付费";
        $lis[30] = "报告付费";
        $lis[40] = "图片付费";
        $lis[50] = "数据付费";
        $lis[60] = "会议付费";
        $lis[61] = "会议报名付费";
        $lis[62] = "会议赞助";
        $lis[70] = "专栏付费";
        $lis[80] = "问答付费可以回答";
        $lis[81] = "问答付费查看答案";
        $lis[90] = "问卷付费";
        return $lis[$k];
    }

    //资金应用场景
    private function scene($k)
    {
        $lis[1] = "视频";
        $lis[2] = "音频";
        $lis[3] = "报告";
        $lis[4] = "图片";
        $lis[5] = "快讯";
        $lis[6] = "数据";
        $lis[7] = "会议";
        $lis[8] = "专栏";
        $lis[9] = "问答";
        $lis[10] = "问卷";
        $lis[11] = "充值";
        $lis[12] = "提现";
        return $lis[$k];
    }



    //资金记录
    public function moneylist()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }


        $id=I('id');
        $m=M('money_record');
        $where['uid']=$id;
        $res=$m->where($where)->order("id desc")->select();
        foreach ($res as &$vo){
            $hh=M('user')->where("id = {$id}")->getField("nickname");
            $vo['name']=$hh;
            $vo['create_time']=date('Y-m-d H:i:s',$vo['create_time']);
            if ($vo['type']==1){
                $vo['type']="收入";
            }elseif($vo['type']==2){
                $vo['type']="支出";
            }elseif ($vo['type']==3){
                $vo['type']="充值";
            }elseif ($vo['type']==4){
                $vo['type']="提现";
            }
            $vo['code']=$this->code($vo['code']);
            $vo['scene']=$this->scene($vo['scene']);
        }
        $this->assign('res',$res);
        $this->display();
    }


    //实名认证
    public function catReal()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        $m = M('user');
        $map['id'] = array('eq', $id);
        $res = $m->where($map)->find();
        $this->assign('info', $res);
        //var_dump($res);
        $this->display();
    }

    //通过实名认证
    public function passReal()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        $m = M('user');
        $map['id'] = array('eq', $id);
        $save = array(
            'realname_status' => 1
        );
        $res = $m->where($map)->save($save);
        if ($res) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //拒绝实名认证
    public function disReal()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        $m = M('user');
        $map['id'] = array('eq', $id);
        $save = array(
            'realname_status' => 2
        );
        $res = $m->where($map)->save($save);
        if ($res) {
            $this->success('操作成功！');
        } else {
            $this->error('操作失败！');
        }
    }

    //用户状态
    public function catFx()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        if ($id) {
            $m = M('user');
            $map['id'] = array('eq', $id);
            $res = $m->where($map)->find();
            $this->assign('info', $res);
            $this->display();
        }
    }

    //禁用用户状态
    public function fxDis()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        if ($id) {
            $m = M('user');
            $map = array(
                'status' => 0
            );
            $where['id'] = array('eq', $id);
            $res = $m->where($where)->save($map);
            if ($res) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
    }

    //正常化用户
    public function fxPass()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        if ($id) {
            $m = M('user');
            $map = array(
                'status' => 1
            );
            $where['id'] = array('eq', $id);
            $res = $m->where($where)->save($map);
            if ($res) {
                $this->success('操作成功！');
            } else {
                $this->error('操作失败！');
            }
        }
    }

    //显示状态
    private function show_type($k)
    {
        $lis[1] = '显示名称';
        $lis[2] = '显示昵称';
        return $lis[$k];
    }

    //用户详情
    public function details()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        if ($id) {
            $m = M('user');
            $map['id'] = array('eq', $id);
            $res = $m->where($map)->find();
            $where['id'] = $res['t_id'];
            $tt = $m->where($where)->getField("nickname");
            $res['t_id'] = $tt;
            $res['bo_status'] = $this->bo_status($res['bo_status']);
            $res['sta'] = $this->sta($res['status']);
            $res['realname_status'] = $this->realname_status($res['realname_status']);
            $res['user_type'] = $this->user_type($res['user_type']);
            $res['source'] = $this->source($res['source']);
            $res['role'] = $this->role($res['role']);
            $res['sex'] = $this->sex($res['sex']);
            $res['show_type'] = $this->show_type($res['show_type']);
            $res['reg_time'] = date('Y/m/d H:i:s', $res['reg_time']);
            $res['last_time'] = date('Y/m/d H:i:s', $res['last_time']);
            $res['birthdaytime'] = date('Y/m/d H:i:s', $res['birthdaytime']);
            $this->assign('list', $res);
            $this->display();
        }
    }


    /**
     * 订单支付状态
     * @param $k
     * @return mixed
     */
    private function status($k)
    {
        $lis[0] = '待支付';
        $lis[1] = '已支付';
        return $lis[$k];
    }

    /**
     * 是否开具发票
     * @param $k
     * @return mixed
     */
    private function isinvoice($k)
    {
        $lis[0] = '否';
        $lis[1] = '已开';
        return $lis[$k];
    }

    /**
     * 支付方式
     * @param $k
     * @return mixed
     */
    private function pay_type($k)
    {
        $lis[1] = '微信';
        $lis[2] = '支付宝';
        $lis[3] = '银联';
        $lis[4] = '账户余额';
        $lis[5] = '其他';
        return $lis[$k];
    }

    /**
     * 订单类型
     */
    private function type($k)
    {
        $lis[1] = '视频';
        $lis[2] = '直播';
        $lis[3] = '音频';
        $lis[4] = '报告';
        $lis[5] = '图片';
        $lis[6] = '快讯';
        $lis[7] = '数据';
        $lis[8] = '会议';
        $lis[9] = '专栏';
        $lis[10] = '问答';
        $lis[11] = '问卷';
        $lis[12] = '购买会员';
        return $lis[$k];
    }

    //用户订单
    public function orderDetails(){
        $id=I('id');
        $map['uid']=$id;
        $moudel=M('order');
        $count=$moudel->where($map)->count();
        $page=new \Think\Page($count,10);
        $show=$page->show();
        $ret=$moudel->where($map)->limit($page->firstRow.','.$page->listRows)->order("create_time desc")->select();

        $use = M('user');
        foreach ($ret as &$val) {
            $val['pay_time'] = date('Y/m/d H:i:s', $val['pay_time']);
            $val['deadline'] = date('Y/m/d H:i:s', $val['deadline']);
            $val['create_time'] = date('Y/m/d H:i:s', $val['create_time']);
            $val['statuss'] = $this->status($val['status']);
            $val['isinvoice'] = $this->isinvoice($val['isinvoice']);
            $val['pay_type'] = $this->pay_type($val['pay_type']);
            $val['type'] = $this->type($val['type']);
            $mp['id'] = $val['uid'];
            $pay = $use->where($mp)->getField("nickname");
            $val['uid'] = $pay;
        }
        $this->assign('list',$ret);
        $this->assign('show',$show);
        $this->display();

    }


    //删除
    public function del()
    {
        //判断是否有合法登录
        if (!$_SESSION['adminster']) {
            $this->error('非法请求！请先登录！', U('Admin/Login/index'), 2);
            return false;
        } else {
            $model = M('admin');
            $map['id'] = $_SESSION['adminster']['id'];
            $res = $model->where($map)->getField("ticket");
            if ($res != $_SESSION['adminster']['ticket']) {
                $this->error('账号已在他处登录！需要重新登录！', U('Admin/Login/index'), 2);
                return false;
            }
        }

        $id = I('id');
        if ($id) {
            $res = M('user')->where("id = $id")->delete();
            $this->success('删除成功！');
        }
    }

}